﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using Jalal.Library.Utility;
using BCGC.BLL.BusinessObject.Resource;
using BCGC.BLL.BusinessObject;
using System.IO;

public partial class Admin_Resource_OtherResource_EditOtherResource : AdminBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.OtherResources);
        }
    }

    private void LoadData()
    {

        ddlGameTitle.DataSource = Game.GetAllGames();
        ddlGameTitle.DataTextField = "GameTitle";
        ddlGameTitle.DataValueField = "GameId";
        ddlGameTitle.DataBind();

        ddlGameTitle.Items.Add(new ListItem("Create New Game", "NewGame"));
        ddlGameTitle.Items.Insert(0, new ListItem("Select Game", "-1"));
        ddlGameTitle.SelectedIndex = 0;

        pnlAddGame.Visible = false;
        tbxNewGame.Text = string.Empty;

        OtherResource otherResource = OtherResource.GetOtherResourceById(Id);
        if (otherResource == null)
            Response.Redirect(CustomSiteMap.OtherResources);
        else
        {
            FileInfo fileInfo = new FileInfo(Server.MapPath(otherResource.FileUrl));

            tbxTitle.Text = otherResource.Title;
            ddlGameTitle.SelectedValue = otherResource.RelatedGame.GameId.ToString();
            hlkDownloadOtherResourceFile.Text = fileInfo.Name;
            hlkDownloadOtherResourceFile.NavigateUrl = otherResource.FileUrl;
            fckDescription.Value = Server.HtmlDecode(otherResource.Description);
            cbxActive.Checked = otherResource.IsActive;
        }
    }

    protected void AddNewGame_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (Game.Create(Security.BindStringInput(tbxNewGame.Text, SecurityLevel.Three)))
            {
                uccResponse.SetSuccessMessage("Game successfuly created");
                LoadData();
            }
            else
                uccResponse.SetFailMessage("Failed to create game");
        }
    }

    protected void GameTitle_Changed(object sender, EventArgs e)
    {
        if (ddlGameTitle.SelectedValue == "NewGame")
            pnlAddGame.Visible = true;
        else
            pnlAddGame.Visible = false;
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (OtherResource.Update(Id, Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
                Security.BindIntegerInput(ddlGameTitle.SelectedValue, SecurityLevel.Three),
                Security.BindStringInput(fckDescription.Value, SecurityLevel.Three),
                Security.BindStringInput(UploadOtherResourceFile(), SecurityLevel.Three), cbxActive.Checked))
            {
                LoadData();
                uccResponse.SetSuccessMessage("Cheat code successfuly updated");
            }
            else
                uccResponse.SetFailMessage("Failed to update cheat code");
        }
    }

    private string UploadOtherResourceFile()
    {
        if (fluOtherResourceFile.HasFile)
        {
            return OtherResource.UploadOtherResourceFile(fluOtherResourceFile.PostedFile);
        }
        else
            return hlkDownloadOtherResourceFile.NavigateUrl;
    }
}
